<template>
  <div class="info-card view-expenses-aggs-card" v-loading="loading">
    <span class="title">{{ title }}</span>
    <div class="money">
      {{ CurrencyFormat.format(expenses.current, props.currency.code) }}
    </div>
    <div class="compare">
      {{ compareTitle }}
      <span :class="expenses.current > expenses.up ? 'up' : 'down'"
        >{{ scale }}
      </span>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { computed } from "vue";
import type { Currency } from "@commons/api/bil_view/type";
import CurrencyFormat from "@commons/utils/currencyFormat";
import PercentFormat from "@commons/utils/percentFormat";
const loading = ref<boolean>(false);
const expenses = ref<{ current: number; up: number }>({ current: 0, up: 0 });
const props = withDefaults(
  defineProps<{
    // 获取数据
    getAggsCount: () => Promise<{ current: number; up: number }>;
    // 标题
    title: string;
    /**
     * 比较标题
     */
    compareTitle: string;

    currency?: Currency;
  }>(),
  {
    currency: () => ({
      code: "CNY",
      message: "人民币",
      symbol: "¥",
      unit: "元",
      exchangeRate: 1,
    }),
  }
);
const scale = computed(() => {
  const s =
    expenses.value.up <= 0
      ? CurrencyFormat.format(expenses.value.current, props.currency.code)
      : PercentFormat.format(
          (expenses.value.current - expenses.value.up) / expenses.value.up
        );
  return expenses.value.current > expenses.value.up ? "+" + s : s;
});
onMounted(() => {
  refresh();
});
const refresh = () => {
  loading.value = true;
  props
    .getAggsCount()
    .then((count) => {
      expenses.value = count;
    })
    .finally(() => {
      loading.value = false;
    });
};
defineExpose({ refresh });
</script>
<style lang="scss" scoped>
.view-expenses-aggs-card {
  height: 94px;
}
.info-card {
  background: #ffffff;
  border-radius: 4px;
  padding: 24px;
  overflow: hidden;
  .title {
    margin: 4px 0 0 4px;
    height: 22px;
    font-size: 14px;
    font-weight: 500;
    color: #1f2329;
  }
  .money {
    margin-top: 6px;
    font-size: 28px;
    line-height: 36px;
    color: #1f2329;
  }
  .compare {
    margin-top: 7px;
    height: 22px;
    color: #1f2329;
    line-height: 22px;
  }
}
.up {
  color: rgba(245, 74, 69, 1);
  font-weight: 400;
  font-size: 16px;
  line-height: 24px;
}
.down {
  color: rgba(52, 199, 36, 1);
  font-weight: 400;
  font-size: 16px;
  line-height: 24px;
}
</style>
